package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Timageinfo;
import com.xy.p2p.service.TimageinfoService;



@Controller
public class TimageinfoAction{
	private static final Logger log = LoggerFactory.getLogger(TimageinfoAction.class);
	public TimageinfoAction() {
	}
 
	//批量审核用户资料
	@RequestMapping("v_timageinfoAudit.do")
	public String Audit(long[] ids, String opr, HttpServletRequest request, ModelMap model) {
		for (long tiid : ids) {
			Timageinfo timageinfo = timageinfoService.get(tiid);
			if ("y".equals(opr)) {
				timageinfo.setStatus(1);
			} else {
				timageinfo.setStatus(-1);
			}
			timageinfoService.save(timageinfo);
		}
		setDict(model);
		return "redirect:v_timageinfoList.do";
	}
 
 	/**
	 * 用户资料表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_timageinfoAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("timageinfo", new Timageinfo());
        setDict(model);
		return "timageinfoEdit";
	}

	/**
	 * 用户资料表新增保存
	 * 
	 * @param timageinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_timageinfoSave.do")
	public String save(Timageinfo timageinfo, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(timageinfo, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		timageinfo = timageinfoService.save(timageinfo);
		return "redirect:v_timageinfoList.do";
	}

	/**
	 * 用户资料表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_timageinfoDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				timageinfoService.remove(id);
			}
		}
		return "redirect:v_timageinfoList.do";
	}

	/**
	 * 用户资料表编辑页面
	 * 
	 * @param tiid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_timageinfoEdit.do")
	public String edit(long tiid, HttpServletRequest request, ModelMap model) {
		Timageinfo timageinfo = timageinfoService.get(tiid);
		model.addAttribute("timageinfo", timageinfo);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "timageinfoEdit";
	}

	/**
	 * 用户资料表详情
	 * 
	 * @param tiid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_timageinfoDetail.do")
	public String detail(long tiid, HttpServletRequest request, ModelMap model) {
		Timageinfo timageinfo = timageinfoService.get(tiid);
		model.addAttribute("timageinfo", timageinfo);
        setDict(model);
		return "timageinfoView";
	}

	/**
	 * 用户资料表查询（分页）
	 * 
	 * @param timageinfo
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_timageinfoList.do")
	public String listPage(Timageinfo timageinfo, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = timageinfoService.getTimageinfoByPage(toMap(timageinfo),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "timageinfoList";
	}

	/**
	 * 用户资料表查询
	 * 
	 * @param timageinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_timageinfoList1.do")
	public String list(Timageinfo timageinfo, HttpServletRequest request, ModelMap model) {
		List<Timageinfo> list = timageinfoService.getTimageinfo(toMap(timageinfo));
		model.addAttribute("list", list);
        setDict(model);
		return "timageinfoList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param timageinfo
	 * @return
	 */	private Map<String,Object> toMap(Timageinfo timageinfo){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(timageinfo.getTiid()!=null&&!timageinfo.getTiid().equals("")){
			map.put("Tiid", timageinfo.getTiid());
		}
		if(timageinfo.getTmember()!=null){
			map.put("Tmember", timageinfo.getTmember());
		}
		if(timageinfo.getPath()!=null&&!timageinfo.getPath().equals("")){
			map.put("Path", timageinfo.getPath());
		}
		if(timageinfo.getContent()!=null&&!timageinfo.getContent().equals("")){
			map.put("Content", timageinfo.getContent());
		}
		if(timageinfo.getRemark()!=null&&!timageinfo.getRemark().equals("")){
			map.put("Remark", timageinfo.getRemark());
		}
		if(timageinfo.getStatus()!=null&&!timageinfo.getStatus().equals("")){
			map.put("Status", timageinfo.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TIMAGEINFO_STATUS));
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Timageinfo timageinfo, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TimageinfoService timageinfoService;   
}